<?php
###########################################################################
#  Copyright (C) 2006-2007 Glyphix, Inc. <briz@glyphix.com>               #
#                                                                         #
#  Permission is hereby granted, free of charge, to any person obtaining  #
#  a copy of this software and associated documentation files (the        #
#  "Software"), to deal in the Software without restriction, including    #
#  without limitation the rights to use, copy, modify, merge, publish,    #
#  distribute, sublicense, and/or sell copies of the Software, and to     #
#  permit persons to whom the Software is furnished to do so, subject to  #
#  the following conditions:                                              #
#                                                                         #
#  The above copyright notice and this permission notice shall be         #
#  included in all copies or substantial portions of the Software.        #
#                                                                         #
#  Except as contained in this notice, the name(s) of the above           #
#  copyright holders shall not be used in advertising or otherwise to     #
#  promote the sale, use or other dealings in this Software without       #
#  prior written authorization.                                           #
#                                                                         #
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        #
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     #
#  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. #
#  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR      #
#  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,  #
#  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  #
#  OTHER DEALINGS IN THE SOFTWARE.                                        #
###########################################################################

/**
 * Singleton factory.
 *
 * Extend this class and call classname::getInstance() to ensure there is only one instance of your object.
 * @return object the unique instance
 */
abstract class SingletonPattern {

    /**
	 * HOW TO USE
	 *
	 * Inherit(extend) from Singleton and add getter:
	 *
	 * //public getter for singleton instance
	 * public static function getInstance(){
	 *   return SingletonPattern::getSingleton(get_class());
	 * }
	 */

    private static $cache = array();

    //protected getter for singleton instances
    protected static function getSingleton( $name ){
        if( !isset(self::$cache[$name]) ){

            $obj = new $name;
            //Make sure this object inherit from Singleton
            if( $obj instanceof SingletonPattern ){
                self::$cache[$name] = $obj;
            }else{
                throw SingletonException( 'Class "' .$className .'" does not inherit from Singleton' );
            }
        }

        return self::$cache[$name];
    }

    //protected constructor to prevent outside instantiation
    protected function __construct(){}

    //deny cloning of singleton objects
    public final function __clone(){
        trigger_error( 'It is impossible to clone a class defined as a Singleton', E_USER_ERROR );
    }

}

/**
 * autoload
 *
 * This changes the behavior of PHP when it looks for a class to instantiate.
 * @author		Brad Brizendine <briz@glyphix.com>
 * @link		http://www.glyphix.com/
 * @license		http://opensource.org/licenses/bsd-license.php BSD License
 * @version		1.0
 * @package		GXPage
 * @subpackage	Engine
 * @copyright	Copyright 2006 - 2007, Glyphix, Inc.
 */
function __autoload( $name = null ){
	// use php's autosearch include functionality; just append .php to the classname
	if( !@include_once($name .'.php') ){
		// if not, force search in GXPage
		if( !@include_once('GXPage' .DIRECTORY_SEPARATOR .$name .'.php') ){
			trigger_error( 'Unable to include ' .$name, E_USER_ERROR );
			return false;
		}
	}
	return true;
}

function showval( $val = null ){
	echo "<hr/>\n";
	if($val){
		echo "<pre>\n";
		echo val2str($val);
		echo "</pre>\n";
	}else{
		echo 'false: [' .($val .'') .'] (' .gettype($val) .')';
	}
}

function dump( $var = null ){
	ob_start();
	var_dump($var);
	$content = ob_get_contents();
	ob_end_clean();
	return $content;
}

function val2str( $val = null ){
	if( !$val ){
		$str = var_export($val,true);
	}else{
		switch(gettype($val)){
			case 'object':
				$str = var_export($val,true);
				break;

			case 'array':
				$str = print_r($val,true);
				break;

			default:
				$str = $val;
		}
	}
	return $str;
}

function arr2obj( $ar = null ) {
	$tmp = new stdClass; // start off a new (empty) object
	foreach( $ar as $key => $value ) {
		if( is_array($value) ){ // if it's multi-dimentional, keep going
			$tmp->$key = arr2obj( $value );
		}else{
			if( is_numeric($key) ){ // can't do it with numbers
				$key = '_' .$key;
			}
			$tmp->$key = $value;
		}
	}
	return $tmp; // return the object!
}

?>